<template>
  <div></div>
</template>

<script>
import { mapActions } from "vuex";
import service from "@/libs/service.js";

export default {
  data() {
    return {};
  },
  created() {
    this.$Spin.show();
    this.ifqywx();
  },
  methods: {
    ...mapActions(["handleLogin", "getUserInfo", "handlegoLogin", "qywxzddl"]),
    ifqywx() {
      var urll = window.location.href;
      var codede = urll.split("code=");
      if (codede[1] == undefined) {
        if (process.env.NODE_ENV === "prerelease") {
          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww1add0f813968d635&redirect_uri=https%3A%2F%2Fpreshoufei.yzxsaas.com%2F%23%2Floading&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        } else if (process.env.NODE_ENV === "production") {
          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww1add0f813968d635&redirect_uri=https%3A%2F%2Fpreshoufei.yzxsaas.com%2F%23%2Floading&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        }
      } else {
        if (this.$route.query.code) {
          this.handleCodeSubmitbendi({ authCode: this.$route.query.code });
        }
      }
    },
    // 企业微信自动登录
    handleCodeSubmitbendi(data) {
      if (data) {
        this.qywxzddl({ code: data.authCode })
          .then((resLogin) => {
            if (resLogin.code === 20000) {
              localStorage.setItem(
                "ifaccountyesnoQYWX",
                resLogin.data.thirdType
              );
              this.$store.commit("print/setPrint", {
                ifaccountyesnoQYWX: localStorage.getItem("ifaccountyesnoQYWX"),
              });
              this.getUserInfo(resLogin)
                .then((res) => {
                  // 获取角色路由
                  this.APIS.getMenuList()
                    .then((res) => {
                      console.log('获取角色路由', res.data);
                      service.axios(res.data);
                      this.$router.replace({
                        path: this.$config.homeName,
                      });
                      localStorage.setItem("menus", this.$config.homeName);
                      this.$Spin.hide();
                      this.loginLoading = false;
                      this.gologin = true;
                    })
                    .catch((err) => {
                      this.loginLoading = false;
                      removeToken();
                    });
                })
                .catch(() => {
                  this.loginLoading = false;
                });
            } else {
              this.loginLoading = false;
            }
          })
          .catch((catchres) => {
            this.loginLoading = false;
          });
      }
    },
  },
};
</script>

<style lang="less">
</style>
